/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.admin.entity.MaintainPlanDeferHistory;
import org.springblade.modules.admin.vo.MaintainPlanDefertHistoryVO;
import org.springblade.modules.admin.mapper.MaintainPlanDefertHistoryMapper;
import org.springblade.modules.admin.service.IMaintainPlanDeferHistoryService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.vo.defert.PlanDeferStatisticVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 维保任务延期历史日志 服务实现类
 *
 * @author ysq
 * @since 2022-11-19
 */
@Service
public class MaintainPlanDeferHistoryServiceImpl extends BaseServiceImpl<MaintainPlanDefertHistoryMapper, MaintainPlanDeferHistory> implements IMaintainPlanDeferHistoryService {

	@Override
	public IPage<MaintainPlanDefertHistoryVO> selectMaintainPlanDeferHistoryPage(IPage<MaintainPlanDefertHistoryVO> page, MaintainPlanDefertHistoryVO maintainPlanDefertHistory) {
		return page.setRecords(baseMapper.selectMaintainPlanDefertHistoryPage(page, maintainPlanDefertHistory));
	}


	@Override
	public PlanDeferStatisticVo getPlanDeferStatistic() {
		Date endTime = DateUtil.endOfDay(new Date());
		Date beginTime =DateUtil.beginOfDay(DateUtil.offsetDay(endTime, -29)); ;
		PlanDeferStatisticVo planDeferStatisticVo = new PlanDeferStatisticVo();
		List<MaintainPlanDeferHistory> maintainPlanDeferHistories = this.list(Wrappers.<MaintainPlanDeferHistory>lambdaQuery().ne(MaintainPlanDeferHistory::getDefertType, 0).between(MaintainPlanDeferHistory::getCreateTime, beginTime, endTime));
		int planCounts = maintainPlanDeferHistories.stream().map(MaintainPlanDeferHistory::getPlanId).distinct().collect(Collectors.toList()).size();
		planDeferStatisticVo.setPlanCounts(planCounts);
		planDeferStatisticVo.setUpdateCounts(maintainPlanDeferHistories.size());
		final List<MaintainPlanDeferHistory> yearPlanList = maintainPlanDeferHistories.stream().filter(o -> o.getPlanType() == 2).collect(Collectors.toList());
		final int yearPlanCounts = yearPlanList.stream().map(MaintainPlanDeferHistory::getPlanId).distinct().collect(Collectors.toList()).size();
		planDeferStatisticVo.setYearPlanCounts(yearPlanCounts);
		planDeferStatisticVo.setYearUpdateCounts(yearPlanList.size());

		final List<MaintainPlanDeferHistory> disYearPlanList = maintainPlanDeferHistories.stream().filter(o -> o.getPlanType() == 1).collect(Collectors.toList());
		final int disYearPlanCounts = disYearPlanList.stream().map(MaintainPlanDeferHistory::getPlanId).distinct().collect(Collectors.toList()).size();

		planDeferStatisticVo.setDisYearPlanCounts(disYearPlanCounts);
		planDeferStatisticVo.setDisYearUpdateCounts(disYearPlanList.size());
		return planDeferStatisticVo;
	}

}
